﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Linq;
using Business_Object;
using Models;

namespace BusinessService.MyPlace
{
    public partial class MyPlaceFavarite : System.Web.UI.Page
    {
        private DichVuService dichVuService = new DichVuService();
        private DiaDiemService diaDiemService = new DiaDiemService();
        private TaiKhoanService taiKhoanService = new TaiKhoanService();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["username"] != null)
            {

                String username = Session["username"].ToString();
                TaiKhoan taiKhoan = taiKhoanService.FindByUserName(username);
                if (taiKhoan != null)
                {
                    List<DiaDiem> diaDiems = diaDiemService.FindByAccount(taiKhoan.MaTaiKhoan);
                    if (diaDiems.Count > 0)
                    {

                        TreeNode treeNode = new TreeNode("Dịch Vụ");
                        TreeView1.Nodes.Add(treeNode);
                        Dictionary<DichVu, List<DiaDiem>> mapDiaDiem = new Dictionary<DichVu, List<DiaDiem>>();
                        List<DichVu> dvs = dichVuService.All();

                        foreach (DichVu dichVu in dvs)
                        {
                            List<DiaDiem> dds = new List<DiaDiem>();
                            foreach (DiaDiem diaDiem in diaDiems)
                            {
                                if (dichVu.MaDichVu == diaDiem.MaDichVu)
                                {
                                    dds.Add(diaDiem);
                                }
                            }
                            if (dds.Count > 0)
                            {
                                mapDiaDiem.Add(dichVu, dds);
                            }
                        }

                        for (int i = 0; i < mapDiaDiem.Count; i++)
                        {
                            TreeNode dvNode = new TreeNode(mapDiaDiem.Keys.ElementAt(i).TenDichVu);
                            treeNode.ChildNodes.Add(dvNode);

                            List<DiaDiem> diems = mapDiaDiem.Values.ElementAt(i);
                            foreach (DiaDiem dd in diems)
                            {
                                TreeNode ddNode = new TreeNode(dd.TenDiaDiem);
                                ddNode.NavigateUrl = "MyPlaceFavorite.aspx";
                                dvNode.ChildNodes.Add(ddNode);
                            }

                        }

                        String filePath = HttpContext.Current.Server.MapPath("~/MyPlace/DiaDiems.xml");

                        XmlParser xmlParser = new XmlParser();
                        XmlDocument doc = xmlParser.ConvertObjectToXml(diaDiems);
                        doc.Save(filePath);
                        Response.Write(doc.InnerXml);
                    }
                }
            }
        }
      
    }
}
